define(function(require, exports, module) {

  var common = require('common'),
      dialog = require('dialog'),
      lang = require('lang');

  var View = require('main/admin/core/Form').extend({
    Model: Backbone.Model.extend({
      idAttribute: 'objectId',
      urlRoot: common.url.ucenter.password,
      validate: function(attrs, options) {
        if (_.isEmpty(attrs.oldPassword)) return lang.get('oldPWD','ucenter') + lang.get('not_allowed_empty');
        if (_.isEmpty(attrs.newPassword)) return lang.get('newPWD','ucenter') + lang.get('not_allowed_empty');
        if (_.isEmpty(attrs.newPassword2)) return lang.get('PWD2','ucenter') + lang.get('not_allowed_empty');
        if(attrs.newPassword != attrs.newPassword2) return lang.get('same_pwd', 'ucenter');
      }
    }),
    template: require('main/admin/ucenter/view/password.handlebars'),
    initialize: function(options) {
      this.model = new this.Model({
        objectId: 0,
        oldPassword: '',
        newPassword: ''
      });
    },
    events: {
      'click .btn-submit': 'onSubmit'
    },
    prepareViewData: function() {
      this.viewData = {
        label: common.ucenter.password.label,
        placeholder: common.ucenter.password.placeholder,
        button: {
          save: lang.get('save')
        }
      };
    },
    prepareSubmitData: function() {
      var data = $(this.el).serializeJSON();
      this.submitData = {
        oldPassword: data.oldPassword,
        newPassword: data.newPassword,
        newPassword2: data.newPassword2
      };
    },
    onSubmit: function(e) {
      this.submit({
        success: function(obj, resp, opt) {
          //提交成功
          dialog.alert(lang.get('pwd_changed','ucenter'), lang.get('save') + lang.get('success'));
        },
        error: function(obj, resp, opt) {
          //提交失败
          dialog.alert(lang.getError(resp.responseJSON.code), lang.get('save') + lang.get('failed'));
        },
        inValid: function(message) {
          //验证对象失败
          dialog.alert(message, lang.get('validate') + lang.get('failed'));
        }
      });
      this.render();
    }
  });

  module.exports = function(options) {
    return ( new View(options) );
  }
});